from django.forms import model_to_dict
from rest_framework import serializers


from rest_framework_simplejwt.serializers import TokenObtainPairSerializer

from chat_api.models import Friend


class MyTokenObtainPairSerializer(TokenObtainPairSerializer):
    def validate(self, attrs):
        data = super().validate(attrs)
        refresh = self.get_token(self.user)
        data['refresh'] = str(refresh)
        data['access'] = str(refresh.access_token)
        data['token'] = data['access']
        del data['access']
        del data['refresh']
        data['data'] = model_to_dict(self.user)
        del data['data']['password']
        return data


class FriendSerializer(serializers.ModelSerializer):
    def to_representation(self, instance):
        ret = super().to_representation(instance)
        ret['user1'] = model_to_dict(instance.user1, exclude=['password'])
        ret['user2'] = model_to_dict(instance.user2,  exclude=['password'])
        return ret

    class Meta:
        model = Friend
        fields = '__all__'